package com.yjps.company.service;

import com.yjps.common.page.Pager;
import com.yjps.company.controller.DayAttentionController;
import com.yjps.company.dao.DayAttentionDao;
import com.yjps.company.model.DayAttention;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 麒全 on 2015/8/18.
 */
@Service
public class DayAttentionService {
    @Autowired
    private DayAttentionDao mDADao;

        private static final Logger log = LoggerFactory.getLogger(DayAttentionService.class);


    //增
    public void add(DayAttention dayAttention){
        mDADao.save(dayAttention);

    }

    //删
    public void delete(int id){
        mDADao.deleteById(id);
    }

    //查
    public DayAttention find(int id){
        String hql = "from DayAttention da where da.id = :id";
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        return (DayAttention)mDADao.findUniqueResult(hql, map);
    }

    //改
    public DayAttention update (DayAttention da){
        mDADao.update(da);
        String hql = "update DayAttention da set da.title = :title , da.content = :content where da.id = :id";
        Map<String,Object> params = new HashMap<>();
        params.put("title",da.getTitle());
        params.put("content",da.getContent());
        params.put("id",da.getId());
        mDADao.excute(hql,params);
        log.debug("update the dayAttention success, id is " + da.getId());
        return mDADao.load(da.getId());

    }



    //获取
    public List<DayAttention> getList(){
        String hql = "from DayAttention da";

        return mDADao.find(hql);

    }

    public Pager<DayAttention> getDayAttentionByPager(Pager<DayAttention> pager) throws Exception {
        Map<String, Object> params = new HashMap<>();
        String queryString = "from DayAttention t where t.isValid = 1";
        queryString += " order by t.id desc";
        List<DayAttention> accounts = mDADao.findWithSelect(queryString, params, (pager.getPageNo()-1)*pager.getPageSize(), pager.getPageSize());
        pager.setCount(mDADao.getTotalCount(queryString, params));
        pager.setList(accounts);
        return pager;
    }
}
